After successfully completing your first exercises to make yourself familiar with R and Rstudio, listening to lengthy lectures on data types, formats, functions, and loops, it’s now time to program a little bit. Surely, we cannot cover all the lecture topics, but you should get a good feeling for (and about) programming. While it can get involved, don’t worry; the first start is not that hard.

This being said, let’s start with some casual tinkering with different data types.

1

Create at least 3 different data formats containing different data types and put them in a list.
Data formats are the Tupperware containers, such as c() or matrix(). Data types, e.g., can be numeric, character strings, and so forth. Please also add another list in this list to create a nested one.

Ok. That’s quite a mishmash (I am so happy this word exists) of different data, right? It’s not easy to grasp which list element is of what data format and what data type. Sometimes we, therefore, have to query this information and print it out. I am sure you can build a loop for that.

2

Have a look at this vector numeric_vector <- c(1, 2, 3, "four"). Create an if-else loop that checks whether this vector really comprises numeric values, prints out a corresponding message if yes, and a different message, if it does not comprise numeric values.
You can check whether a vector is numeric with the is.numeric() function.

That’s nice but static. We have to repeatedly write this statement if we want to check multiple objects. Let’s write a function.

3

Take your if-else loop and make a function out of it. Also, try it out on more than one object.
Ensure your function arguments in function () match the ones within the curly brackets {}.